const events=require('events');
const {User}=require('../models/index');

module.exports=(verify=true)=>async (ctx,next)=>{
    let {token}=ctx.query,ee=new events();
    ee.on('invalid',msg=>{
        ctx.body={
            code:400,
            msg
        }
    });

    let tokenUser=await User.findOne({token},{_id:true});
    if(!tokenUser)return ee.emit('invalid','Token错误');
    ctx.state.token=token;
    ctx.state.tokenUser=tokenUser;
    await next();
};